package cn.edu.scnu.semart.goods.controller;

import cn.edu.scnu.semart.goods.service.GoodsAuditService;
import cn.edu.scnu.semart.model.goods.GoodsAudit;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 商品审核Controller
 */
@RestController
@RequestMapping("/audit")
//@CrossOrigin
public class GoodsAuditController {

    @Autowired
    private GoodsAuditService goodsAuditService;

    /**
     * 提交商品审核
     */
    @PostMapping("/submit/{goodsId}")
    public Map<String, Object> submitForAudit(@PathVariable Integer goodsId) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = goodsAuditService.submitForAudit(goodsId);
            result.put("success", success);
            if (success) {
                result.put("message", "提交审核成功");
            } else {
                result.put("message", "提交审核失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 审核通过
     */
    @PostMapping("/approve/{goodsId}")
    public Map<String, Object> approve(
            @PathVariable Integer goodsId,
            @RequestParam Integer auditorId,
            @RequestParam String auditorName,
            @RequestParam(required = false) String remark) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = goodsAuditService.approve(goodsId, auditorId, auditorName, remark);
            result.put("success", success);
            if (success) {
                result.put("message", "审核通过成功");
            } else {
                result.put("message", "审核通过失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 审核拒绝
     */
    @PostMapping("/reject/{goodsId}")
    public Map<String, Object> reject(
            @PathVariable Integer goodsId,
            @RequestParam Integer auditorId,
            @RequestParam String auditorName,
            @RequestParam String rejectReason) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = goodsAuditService.reject(goodsId, auditorId, auditorName, rejectReason);
            result.put("success", success);
            if (success) {
                result.put("message", "审核拒绝成功");
            } else {
                result.put("message", "审核拒绝失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取待审核商品列表
     */
    @GetMapping("/pending")
    public Map<String, Object> getPendingAuditList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            Page<GoodsAudit> page = new Page<>(current, size);
            IPage<GoodsAudit> auditPage = goodsAuditService.getPendingAuditList(page);
            
            result.put("success", true);
            result.put("data", auditPage);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取审核历史
     */
    @GetMapping("/history/{goodsId}")
    public Map<String, Object> getAuditHistory(
            @PathVariable Integer goodsId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            Page<GoodsAudit> page = new Page<>(current, size);
            IPage<GoodsAudit> historyPage = goodsAuditService.getAuditHistory(goodsId, page);
            
            result.put("success", true);
            result.put("data", historyPage);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 获取审核统计
     */
    @GetMapping("/statistics")
    public Map<String, Object> getAuditStatistics() {
        Map<String, Object> result = new HashMap<>();
        try {
            GoodsAuditService.AuditStatistics statistics = goodsAuditService.getAuditStatistics();
            result.put("success", true);
            result.put("data", statistics);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
} 